package com.lyx.bookmanager.controller.user;

import com.lyx.bookmanager.mapper.*;
import com.lyx.bookmanager.pojo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/user/book")
public class UserBookController {
    @Autowired
    private BookMapper bookMapper;

    @Autowired
    GxUserBookCollectMapper gxUserBookCollectMapper;
    @Autowired
    ReaderMapper readerMapper;
    @Autowired
    GxUserBookPayMapper gxUserBookPayMapper;
    @Autowired
    RecordMapper recordMapper;

    @RequestMapping("/getAll")
    public String getAll(Model model) {
        List<Book> books = bookMapper.getAllBook();

        model.addAttribute("books", books);
        return "user/books";
    }

    @RequestMapping("/info")
    public String info(Model model, @RequestParam(value = "id") String id, @RequestParam(value = "username") String username) {
        long bookId = Long.parseLong(id);
        List<GxUserBookCollect> list = gxUserBookCollectMapper.getGxUserBookCollectList(bookId, username);
        Book book = bookMapper.getBookById(id);
        long reader_id = Long.parseLong(username);
        GxUserBookPay gxUserBookPay = gxUserBookPayMapper.selectByPrimaryKey(new GxUserBookPayKey(reader_id, bookId));
        if (gxUserBookPay != null && gxUserBookPay.getReaderId() != null && gxUserBookPay.getBookId() != null) {
            book.setIsBuy(1);
        } else {
            book.setIsBuy(0);
        }

        Record record = recordMapper.getRecordByReaderIdAndBookId(bookId, reader_id);
        if (record != null && record.getLendDate() != null && record.getBackDate() == null) {
            book.setIsLend(1);
        } else {
            book.setIsLend(0);
        }
        book.setRemarkList(list);
        model.addAttribute("book", book);
        return "user/book_info";
    }

    @RequestMapping("/search")
    public String search(String keyword, Model model) {
        List<Book> books = bookMapper.searchBook(keyword);
        model.addAttribute("books", books);
        return "user/book_search_result";
    }

    @RequestMapping("/like")
    @ResponseBody
    public String like(String bookId) {
        Book book = bookMapper.getBookById(bookId);
        int likeCount = book.getLikeCount();
        book.setLikeCount(likeCount + 1);
        bookMapper.updateBook(book);
        System.out.println();
        return "已点赞";
    }

    @RequestMapping("/collect")
    @ResponseBody
    public String like(String bookId, String username) {
        GxUserBookCollect gxUserBookCollect = new GxUserBookCollect();
        long id = Long.parseLong(bookId);
        gxUserBookCollect.setBookId(id);
        gxUserBookCollect.setUsername(username);
        gxUserBookCollect.setIsCollect(1);
        gxUserBookCollect.setCreatetime(new Date());
        GxUserBookCollectExample example = new GxUserBookCollectExample();
        GxUserBookCollectExample.Criteria criteria = example.createCriteria();
        criteria.andBookIdEqualTo(id).andUsernameEqualTo(username).andIsCollectEqualTo(1);
        List<GxUserBookCollect> list = gxUserBookCollectMapper.selectByExample(example);
        if (list.size() == 0) {
            gxUserBookCollectMapper.insert(gxUserBookCollect);
        }
        return "已收藏";
    }

    @RequestMapping("/remark")
    public String remark(String bookId, String username, String remark, Model model) {
        GxUserBookCollect gxUserBookCollect = new GxUserBookCollect();
        long id = Long.parseLong(bookId);
        gxUserBookCollect.setBookId(id);
        gxUserBookCollect.setUsername(username);
        gxUserBookCollect.setRemark(remark);
        gxUserBookCollectMapper.insert(gxUserBookCollect);
        List<GxUserBookCollect> list = gxUserBookCollectMapper.getGxUserBookCollectList(id, username);
        Book book = bookMapper.getBookById(bookId);
        book.setRemarkList(list);
        model.addAttribute("book", book);
        return "user/book_info";
    }
}
